﻿using DotNetMVC.OA.IBLL;
using DotNetMVC.OA.Model;
using DotNetMVC.OA.Model.Param;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DotNetMVC.OA.UI.Portal.Controllers
{
    public class UserInfoController : BaseController
    {
        public IUserInfoService UserInfoService { get; set; }
        public IRoleInfoService RoleInfoService { get; set; }
        public IR_UserInfo_RoleInfoService R_UserInfo_RoleInfoService { get; set; }

        public IActionInfoService ActionInfoService { get; set; }
        public IR_UserInfo_ActionInfoService R_UserInfo_ActionInfoService { get; set; }

        short delFlagNormal = (short)Model.Enum.DelFlagEnum.Normal;
        // GET: UserInfo
        public ActionResult Index()
        {
            return View();
        }

        #region 查询
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllUserInfos()
        {
            //jquery easyui table:{total:50,rows:[{},{}]}
            //easyui table 在初始化的时候自动发送一下两个参数值
            int pageSize = int.Parse(Request["rows"] ?? "10");
            int pageIndex = int.Parse(Request["page"] ?? "1");

            //过滤用户名、备注   
            string schName = Request["schName"];
            string schRemark = Request["schRemark"];
            //初始化查询条件
            var queryParam = new UserQueryParam
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Total = 0,
                SchName = schName,
                SchRemark = schRemark
            };

            var pageData = UserInfoService.LoagPageData(queryParam);

            int total = queryParam.Total;

            #region old
            //short delflagNormal = (short)Model.Enum.DelFlagEnum.Normal;
            ////获取当前页的数据
            //var pageData = UserInfoService.GetPageEntities(pageSize, pageIndex, out total,
            //    u => u.DelFlag == delflagNormal,
            //    u => u.ID,
            //    true)
            //    .Select(u => new { u.ID, u.UName, u.Remark, u.ShowName, u.SubTime, u.ModfiedOn, u.Pwd, u.DelFlag });
            #endregion

            var temp = pageData.Select(u => new { u.ID, u.UName, u.Remark, u.ShowName, u.SubTime, u.ModfiedOn, u.Pwd, u.DelFlag });
            var data = new { total = total, rows = temp.ToList() };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加
        [HttpPost]
        public ActionResult Add(UserInfo userInfo)
        {
            userInfo.DelFlag = delFlagNormal;
            userInfo.ModfiedOn = DateTime.Now;
            userInfo.SubTime = DateTime.Now;

            UserInfoService.Add(userInfo);
            return Content("ok");
        }
        #endregion

        #region 删除
        public ActionResult BatchDelete(string ids)
        {
            if (string.IsNullOrEmpty(ids))
            {
                return Content("请选中要删除的数据");
            }
            string[] strIds = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            List<long> idList = new List<long>();
            foreach (var strId in strIds)
            {
                idList.Add(long.Parse(strId));
            }
            UserInfoService.Delete(idList);
            return Content("ok");
        }

        public ActionResult Delete(long id)
        {
            UserInfoService.Delete(id);
            return Content("ok");
        }
        #endregion

        #region 修改
        [HttpGet]
        public ActionResult Edit(long id)
        {
            //给视图传值
            ViewData.Model = UserInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            return View();
        }

        [HttpPost]
        public ActionResult Edit(UserInfo userInfo)
        {
            UserInfoService.Update(userInfo);
            return Content("ok");
        }
        #endregion

        #region 设置角色
        public ActionResult SetRole(long id)
        {
            //当前要设置角色的用户
            UserInfo user = UserInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            //获取所有角色
            ViewBag.AllRoles = RoleInfoService.GetEntities(u => u.DelFlag == delFlagNormal).ToList();
            //获取用户已有角色
            //ViewBag.ExitsRoles = R_UserInfo_RoleInfoService.GetEntities(r => r.UserInfoID == id && r.DelFlag == delFlagNormal).Select(r=>r.RoleInfoID);
            ViewBag.ExitsRoles = (from r in user.R_UserInfo_RoleInfo
                                  where r.DelFlag == delFlagNormal
                                  select r.RoleInfoID).ToList();
            //给视图传值
            return View(user);
        }

        public ActionResult ProcessSetRole(long UId)
        {
            List<long> setRoleIdList = new List<long>();
            //获取选中name值
            foreach (var key in Request.Form.AllKeys)
            {
                if (key.StartsWith("ckb_"))
                {
                    long roleId = long.Parse(key.Replace("ckb_", ""));
                    setRoleIdList.Add(roleId);
                }
            }
            UserInfoService.SetRole(UId, setRoleIdList);

            return Content("ok");
        }
        #endregion

        #region 设置特殊权限
        public ActionResult SetAction(long id)
        {
            ViewBag.User = UserInfoService.GetEntities(u => u.ID == id).FirstOrDefault();
            ViewData.Model = ActionInfoService.GetEntities(a => a.DelFlag == delFlagNormal).ToList();
            ViewBag.HasActions = R_UserInfo_ActionInfoService.GetEntities(r => r.DelFlag == delFlagNormal).ToList();
            return View();
        }

        /// <summary>
        /// 删除当前用户特殊权限
        /// </summary>
        /// <param name="UId"></param>
        /// <param name="ActionId"></param>
        /// <returns></returns>
        public ActionResult DeleteUsreAction(long UId, long ActionId)
        {
            var rUsreAction = R_UserInfo_ActionInfoService.GetEntities(r => r.UserInfoID == UId
                                                && r.ActionInfoID == ActionId
                                                && r.DelFlag == delFlagNormal).FirstOrDefault();

            if (rUsreAction != null)
            {
                R_UserInfo_ActionInfoService.Delete(rUsreAction);
            }

            return Content("ok");
        }

        /// <summary>
        /// 设置当前用户的特殊权限
        /// </summary>
        /// <param name="UId"></param>
        /// <param name="ActionId"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public ActionResult SetUsreAction(long UId, long ActionId, int Value)
        {
            var rUsreAction = R_UserInfo_ActionInfoService.GetEntities(r => r.UserInfoID == UId
                                                && r.ActionInfoID == ActionId
                                                && r.DelFlag == delFlagNormal).FirstOrDefault();

            if (rUsreAction != null)
            {
                rUsreAction.HasPermission = Value == 1 ? true : false;
                R_UserInfo_ActionInfoService.Update(rUsreAction);
            }
            else
            {
                R_UserInfo_ActionInfo rUsreInfoActionInfo = new R_UserInfo_ActionInfo();
                rUsreInfoActionInfo.UserInfoID = UId;
                rUsreInfoActionInfo.ActionInfoID = ActionId;
                rUsreInfoActionInfo.HasPermission = Value == 1 ? true : false;
                rUsreInfoActionInfo.DelFlag = delFlagNormal;
                R_UserInfo_ActionInfoService.Add(rUsreInfoActionInfo);
            }

            return Content("ok");
        }
        #endregion
    }
}